package map.oj;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Num349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        List<Integer> ret = new ArrayList<>();
        if(nums1.length == 0 || nums2.length == 0){
            return null;
        }
        Set<Integer> set = new HashSet<Integer>();
        for (int num : nums1){
            set.add(num);
        }
        int index = 0;
        Set<Integer> setIndex = new HashSet<Integer>();
        for (int i = 0; i < nums2.length; i++) {
            int num2 = nums2[i];
            if(set.contains(num2)){
                if(setIndex.add(num2)){
                    ret.add(num2);
                }
            }
        }
        int[] intersectionRet = new int[ret.size()];
        int index2 = 0;
        for (int num : ret){
            intersectionRet[index++] = num;
        }
        return intersectionRet;
    }
}
